package com.wuyi.tea.controller.user;

import com.wuyi.tea.domain.dto.ProductDTO;
import com.wuyi.tea.domain.vo.ProductVO;
import com.wuyi.tea.domain.vo.R;
import com.wuyi.tea.service.CartService;
import com.wuyi.tea.util.LocalStorageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = "购物车模块")
@RestController
@RequestMapping("/user/cart")
public class CartController {


    @Resource
    private CartService cartService;

    @ApiOperation("获取购物车列表")
    @GetMapping("/list")
    public R list(){

        log.info("id为{}的用户正在获取购物车商品列表", LocalStorageUtil.get());
        List<ProductVO>productVOS = cartService.getCartProductList();
        return R.success(productVOS);
    }

    @ApiOperation("添加商品到购物车列表")
    @PostMapping
    public R addToCart(@RequestBody ProductDTO productDTO){

        log.info("id为{}的用户正在向购物车添加商品：\n{}",LocalStorageUtil.get(),productDTO);
        cartService.addCartProduct(productDTO);
        return R.success();
    }


    @ApiOperation("批量删除购物车商品")
    @DeleteMapping
    public R deleteList(@RequestParam("ids") List<Integer>ids){

        log.info("id为{}的用户删除了这些商品: \n{}",LocalStorageUtil.get(),ids);
        cartService.deleteCartProductList(ids);
        return R.success("删除成功",null);
    }
}
